//  PULSAR, THE GAME
//  ================================
//  COPYRIGHT(c) 2013 BY KAMIL MRZYGŁÓD
//  CODENOVA.PL

define(function () {
    var container, camera, scene, group, renderer;
    var mouseX = 0, mouseY = 0;

    var windowHalfX = window.innerWidth / 2;
    var windowHalfY = window.innerHeight / 2;

    var Planet = function(objPlanet) {

    };

    Planet.prototype = {
        Add: function() {
            this.Render();
        },

        Create: function(objPlanet) {
            return new Planet(objPlanet);
        },

        Initialize: function() {
            container = document.getElementById( 'pulsarBody' );

            camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 2000 );
            camera.position.z = 500;

            scene = new THREE.Scene();

            group = new THREE.Object3D();
            scene.add( group );

            // earth

            var loader = new THREE.TextureLoader();
            loader.load( '../Resources/Textures/land_ocean_ice_cloud_2048.jpg', function ( texture ) {

                var geometry = new THREE.SphereGeometry( 200, 20, 20 );

                var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: true } );
                var mesh = new THREE.Mesh( geometry, material );
                group.add( mesh );

            } );

            var canvas = document.createElement( 'canvas' );
            canvas.width = 128;
            canvas.height = 128;

            var texture = new THREE.Texture( canvas );
            texture.needsUpdate = true;

            var geometry = new THREE.PlaneGeometry( 1000, 1000, 1, 1 );
            var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: true } );

            var mesh = new THREE.Mesh( geometry, material );
            mesh.position.y = - 250;
            mesh.rotation.x = - Math.PI / 2;
            group.add( mesh );

            renderer = new THREE.CanvasRenderer();
            renderer.setSize( window.innerWidth, window.innerHeight );

            container.appendChild( renderer.domElement );

            document.addEventListener( 'mousemove', this.onDocumentMouseMove, false );

            //

            //window.addEventListener( 'resize', onWindowResize, false );
        },

        onDocumentMouseMove: function ( event ) {
            mouseX = ( event.clientX - windowHalfX );
            mouseY = ( event.clientY - windowHalfY );
        },

        Render: function() {
            camera.position.x += ( mouseX - camera.position.x ) * 0.05;
            camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
            camera.lookAt( scene.position );

            group.rotation.y -= 0.005;

            renderer.render( scene, camera );
        }
    };

    return Planet.prototype.Create;
});